import router from '@/router'
const state = {
  tabList: []
}
const mutations = {
  addTab(state, tab) {
    if (state.tabList.every(item => item.path !== tab.path)) {
      state.tabList.push(tab)
    }
  },
  deleteTab(state, tab) {
    if (tab.isCurrent) {
      state.tabList.forEach((item, index) => {
        if (item.path !== tab.path) return
        const nextTab = state.tabList[index + 1] || state.tabList[index - 1]
        if (!nextTab) {
          router.push('/home')
          return
        }
        router.push(nextTab.path)
      })
    }
    // set tabs
    state.tabList = state.tabList.filter(item => item.path !== tab.path)
  }
}
const actions = {}

const getters = {}
export default {
  state,
  mutations,
  actions,
  getters
}
